#include "nmea_data.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char GNRMC_Head[] = "$GNRMC";

static float sixty_2_ten(float sixty)
{
	int int_part;
	float point_part;
	int_part = sixty / 100;
	point_part = (sixty - int_part * 100) / 60.0;

	return int_part + point_part;
}

signed char nmea_msg_GNRMC(nmea_GNRMC_t *gnrmc_t, unsigned char *data, int len)
{
	char *gnrmc_start_index = strstr((char *)data, GNRMC_Head);
	int gnrmc_line_len = 0;
	int split_num = 0, split_index = 0, last_split_index = 0;

	char *temp = gnrmc_start_index;
	char t_buf[24];

	while(temp) {
		if(',' == temp[gnrmc_line_len]) {

			last_split_index = split_index;
			split_index = gnrmc_line_len;

			switch(split_num) {
				case 1:
					if(split_index - last_split_index > 5) {
						// hhmmss
						gnrmc_t->hour = (temp[last_split_index + 1] - '0') * 10 | (temp[last_split_index + 2] - '0');
						gnrmc_t->min = (temp[last_split_index + 3] - '0') * 10 | (temp[last_split_index + 4] - '0');
						gnrmc_t->sec = (temp[last_split_index + 5] - '0') * 10 | (temp[last_split_index + 6] - '0');
						// printf("%d:%d:%d\r\n", gnrmc_t->hour, gnrmc_t->min, gnrmc_t->sec);
					} else {
						gnrmc_t->hour = 0;
						gnrmc_t->min = 0;
						gnrmc_t->sec = 0;
					}
					break;

				case 2:
					if(split_index - last_split_index > 1) {
						gnrmc_t->valid_state = temp[last_split_index + 1];
						// printf("state:%c\r\n", gnrmc_t->valid_state);
					} else {
						gnrmc_t->valid_state = '\0';
					}
					break;

				case 3:
					if(split_index - last_split_index > 2) {
						// latitude
						memcpy(t_buf, &temp[last_split_index + 1], split_index - last_split_index - 1);
						// gnrmc_t->latitude = atof(t_buf);
						gnrmc_t->latitude = sixty_2_ten(atof(t_buf));
					} else {
						gnrmc_t->latitude = 0;
					}
					// printf("%.2f\r\n", gnrmc_t->latitude);
					break;

				case 4:
					if(split_index - last_split_index > 1) {
						gnrmc_t->latitude_ns = temp[last_split_index + 1];
						// printf("ns:%c\r\n", gnrmc_t->latitude_ns);
					} else {
						gnrmc_t->latitude_ns = '\0';
					}
					break;

				case 5:
					// printf("last is %d, split is %d\r\n", last_split_index, split_index);
					if(split_index - last_split_index > 2) {
						// latitude
						memcpy(t_buf, &temp[last_split_index + 1], split_index - last_split_index - 1);
						// gnrmc_t->longitude = atof(t_buf);
						gnrmc_t->longitude = sixty_2_ten(atof(t_buf));
					} else {
						gnrmc_t->longitude = 0;
					}
					// printf("%.2f\r\n", gnrmc_t->longitude);
					break;

				case 6:
					if(split_index - last_split_index > 1) {
						gnrmc_t->longitude_ew = temp[last_split_index + 1];
						// printf("ew:%c\r\n", gnrmc_t->longitude_ew);
					} else {
						gnrmc_t->longitude_ew = '\0';
					}
					break;

				case 9:
					if(split_index - last_split_index > 5) {
						// hhmmss
						gnrmc_t->day = (temp[last_split_index + 1] - '0') * 10 | (temp[last_split_index + 2] - '0');
						gnrmc_t->month = (temp[last_split_index + 3] - '0') * 10 | (temp[last_split_index + 4] - '0');
						gnrmc_t->year = ((temp[last_split_index + 5] - '0') * 10 | (temp[last_split_index + 6] - '0')) + 2000;
						// printf("%d:%d:%d\r\n", gnrmc_t->day, gnrmc_t->month, gnrmc_t->year);
					} else {
						gnrmc_t->day = 0;
						gnrmc_t->month = 0;
						gnrmc_t->year = 0;
					}
					break;


				default:
					break;
			}

			split_num++;

		}

		if('\r' == temp[gnrmc_line_len] || '\n' == temp[gnrmc_line_len]) {
			break;
		}
		gnrmc_line_len++;
	}

	// printf("%.*s\n", gnrmc_line_len, gnrmc_start_index);

	return 0;
}

